package com.example.demo.controller;

import com.example.demo.entity.User;
import com.example.demo.service.UserService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @RequestMapping("/show_register_page")
    public String show_register_page(){
        return "register";
    }

    @RequestMapping("/doregister")
    public String doRegister(User user, HttpSession session){
        User dbUser = userService.findByUserName(user.getUsername());
        if (dbUser==null){
            userService.create(user);
            session.removeAttribute("errmsg");
            return "redirect:/user/loginpage";
        }
        else {
            session.removeAttribute("errmsg");
            session.setAttribute("errmsg","用户已存在，请重新注册");

            return "redirect:/user/show_register_page";

        }

    }
    @RequestMapping("/loginpage")
    public String loginPage(HttpSession httpSession){
        httpSession.removeAttribute("user");
        return "login";
    }
    @RequestMapping("/dologin")
    public String doLogin(User user ,HttpSession session){
        User dbuser = userService.findByUserName(user.getUsername());
        if (dbuser==null){
            session.removeAttribute("errmsg");
            session.setAttribute("errmsg","用户不存在");
            return "redirect:/user/loginpage";
        }
        else if (dbuser.getPassword().equals(user.getPassword())){

            session.setAttribute("user",dbuser);
            return "redirect:/food/show_food_page";
        }
        else {
            System.out.println(dbuser.getPassword());
            System.out.println(user.getPassword());
            session.removeAttribute("errmsg");
            session.setAttribute("errmsg","密码错误");
            return "redirect:/user/loginpage";
        }
    }
    @RequestMapping("/show_user_page")
    public ModelAndView show_user_page(ModelAndView modelAndView,@RequestParam(defaultValue="1")Integer
            pageNum,@RequestParam(defaultValue="5")Integer pageSize){
        PageInfo<User> userPageInfo=userService.selectUserPage(pageNum,pageSize);
        modelAndView.addObject("userPageInfo",userPageInfo);
        modelAndView.setViewName("user");
        return modelAndView;
    }
    @RequestMapping("/insertUser")
    public String insertUser(){
        return "insertUser";
    }
    @RequestMapping("/doinsertUser")
    @ResponseBody
    public Map<String,Object> doinsertUser(String username,String password){

        Map<String,Object> resultMap = new HashMap<String, Object>();
        User user =userService.findByUserName(username);
        if (user!=null){
            resultMap.put("result","用户已经存在啦");
        }else {
            User userdb=new User();
            userdb.setUsername(username);
            userdb.setPassword(password);
            userService.create(userdb);
            resultMap.put("result","SUCCESS");
        }

        return resultMap;
    }
    @RequestMapping("/updateUser")
    public String updateUser(Model model,String username){
        User user=userService.findByUserName(username);
        int id=user.getId();
        String password=user.getPassword();
        model.addAttribute("id",id);
        model.addAttribute("username",username);
        model.addAttribute("password",password);
        return "updateUser";
    }
    @RequestMapping("/doupdateUser")
    @ResponseBody
    public Map<String,Object> doupdateUser(int id,String username,String password){

        Map<String,Object> resultMap = new HashMap<String, Object>();
        User user =new User();
        User userdb=userService.findByUserName(username);
        if (userdb!=null&&userdb.getId()!=id){
            resultMap.put("result","用户名已存在，换个用户名试试？");
        }
        else {

            user.setId(id);
            user.setUsername(username);
            user.setPassword(password);
            userService.updateUser(user);
            resultMap.put("result","SUCCESS");
        }



        return resultMap;
    }
    @RequestMapping("/deleteUser")
    @ResponseBody
    public Map<String,Object> deleteUser(int id){
        Map<String,Object> resultMap = new HashMap<String, Object>();
        userService.deleteUser(id);
        resultMap.put("result","SUCCESS");
        return resultMap;
    }
}
